package _21_多线程juc.demo03_atomic;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicReference;

public class TestAtomicRef {

    @Test
    public void test01() {
        TestStu stu = new TestStu("abc", 22);
        AtomicReference<TestStu> reference = new AtomicReference<>(stu);
        System.out.println("reference.get().name = " + reference.get().name);
        System.out.println("reference.get().account = " + reference.get().account);

        boolean c = reference.compareAndSet(new TestStu("abc", 22), new TestStu("bcd", 88));
        System.out.println("c = " + c);
        System.out.println("reference.get().name = " + reference.get().name);
        System.out.println("reference.get().account = " + reference.get().account);

        boolean b = reference.compareAndSet(stu, new TestStu("bcd", 88));
        System.out.println("b = " + b);
        System.out.println("reference.get().name = " + reference.get().name);
        System.out.println("reference.get().account = " + reference.get().account);
    }

}

@Data
@NoArgsConstructor
@AllArgsConstructor
class TestStu {
    String name;
    int account;
}